// user.js - 用户信息和token存储模块

const state = {
  token: uni.getStorageSync('token') || '',
  userInfo: uni.getStorageSync('userInfo') || {}
}

const mutations = {
  SET_TOKEN(state, token) {
    state.token = token
    uni.setStorageSync('token', token)
  },
  SET_USER_INFO(state, userInfo) {
    state.userInfo = userInfo
    uni.setStorageSync('userInfo', userInfo)
  },
  CLEAR_USER(state) {
    state.token = ''
    state.userInfo = {}
    uni.removeStorageSync('token')
    uni.removeStorageSync('userInfo')
  }
}

const actions = {
  // 登录成功后调用
  login({ commit }, { token, userInfo }) {
    commit('SET_TOKEN', token)
    commit('SET_USER_INFO', userInfo)
  },
  
  // 退出登录
  logout({ commit }) {
    commit('CLEAR_USER')
  },
  
  // 检查是否登录
  checkLogin({ state }) {
    return !!state.token
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
} 